***********************
*** DFBETA ANALYSIS ***
***********************

	*** Load data
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		drop if year < 1960
		drop if missing(pwt) & missing(wdi) & missing(mad)
		
		reshape wide $datasets, i(ccode country) j(year)
		
	*** Assign groups 
		kountry ccode, from(iso3c) geo(undet)
		gen clusters = "West" if country == "Kosovo" | regexm(GEO, "Europe")==1 | regexm(GEO, "Australia and New Zealand") | inlist(country, "Canada", "United States")
		replace clusters = "Africa" if regexm(GEO, "Africa")==1
		replace clusters = "Latin America" if  GEO == "South America" | GEO == "Caribbean" | GEO == "Central America" 
		replace clusters = "Asia" if regexm(GEO, "Asia")==1 | GEO == "Melanesia" | GEO == "Micronesia" | GEO == "Polynesia" | country == "Taiwan"
		assert !missing(clusters) if !missing(pwt2019)
		
	*** Cycle through regressions
		local j = 1	
		foreach data in pwt {			
			// Customize window by dataset
			local firstyear = 1960
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			local endyear = `lastyear' - 1 		
				forval startyear = `firstyear'(1)`endyear' {
					gen outcome = (log(`data'`lastyear'/`data'`startyear')/(`lastyear' - `startyear'))
					gen initial = log(`data'`startyear')
					gen `data'_x_`startyear' = . 
					gen `data'_lx_`startyear' = . 
					gen `data'_ux_`startyear' = . 
					foreach cluster in "Asia" "West" "Latin America" "Africa" {
						//qui  nl (outcome = {b0=.04} - (1 - exp(-1*{b1=0.001}*(`outcomeyear' - `startyear')))/(`outcomeyear' - `startyear')*initial) if clusters != "`cluster'", vce(robust) 
						qui reg outcome initial if clusters != "`cluster'", robust 
						replace `data'_x_`startyear' = _b[initial] if clusters == "`cluster'"
						replace `data'_lx_`startyear' = _b[initial] - invttail(`e(df_r)',0.025)*_se[initial] if clusters == "`cluster'"
						replace `data'_ux_`startyear' = _b[initial] + invttail(`e(df_r)',0.025)*_se[initial] if clusters == "`cluster'"
					}
					qui reg outcome initial, robust 
					gen `data'_xmain_`startyear' = _b[initial] 
					gen `data'_lxmain_`startyear' = _b[initial] - invttail(`e(df_r)',0.025)*_se[initial] 
					gen `data'_uxmain_`startyear' = _b[initial] + invttail(`e(df_r)',0.025)*_se[initial] 
					drop outcome initial 
				}
		}
		keep *_x_* *_lx_* *_ux_* *xmain_* clusters
	
		duplicates drop 
		reshape long pwt_x_ pwt_lx_ pwt_ux_ pwt_xmain_ pwt_lxmain_ pwt_uxmain_, i(clusters) j(year)
		rename *_ *
		
		gen year2 = year + .2 
		gen year3 = year + .4
		gen year4 = year + .6 
		gen period = 2019 - year 
		foreach var in pwt_x pwt_lx pwt_ux pwt_xmain pwt_lxmain pwt_uxmain {
			replace `var'= -ln(`var'*period + 1)/period 
		}
		
		#delimit ;	
		tw 	(rcap pwt_lxmain pwt_uxmain year4 if inrange(year, 1960, 2010), lcolor(black%30))
			(rcap pwt_lx pwt_ux year if clusters == "Africa" & inrange(year,1960,2010), lcolor(navy%30)) 
			(rcap pwt_lx pwt_ux year2 if clusters == "Asia" & inrange(year,1960,2010), lcolor(maroon%30)) 
			(rcap pwt_lx pwt_ux year3 if clusters == "Latin America" & inrange(year,1960,2010), lcolor(forest_green%30)) 
			(sc pwt_xmain year4 if inrange(year,1960,2010), msymbol(O) mcolor(black%70))
			(sc pwt_x year if clusters == "Africa" & inrange(year,1960,2010) , mcolor(navy%70)) 
			(sc pwt_x year2 if clusters == "Asia" & inrange(year,1960,2010), msymbol(D) mcolor(maroon%70)) 
			(sc pwt_x year3 if clusters == "Latin America" & inrange(year,1960,2010), msymbol(S) mcolor(forest_green%70)), 
			xtitle("Year") 
			ytitle("{&beta}", orientation(horizontal) size(large)) 
			plotregion(style(none) lcolor(none))  
			xlabel(1960(5)2010) 
			graphregion(fcol(white) lcol(white)) 
			legend(order(6 "World Minus Africa" 7 "World Minus Asia" 8 "World Minus Latin America" 5 "Full Sample") cols(1) ring(0) pos(10) bmargin(small) region(color(none))) 
			yline(0, lcolor(gs10) lpattern(dash))
		;
		#delimit cr
		graph export "$figures/dfbeta.pdf", replace 
